﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using SCResources.DataLayer;
using SCResources.Interfaces;
using SCResources.General;
using Timer = System.Timers.Timer;

namespace SCResources.BusinissLayer
{
    internal class FileMoving
    {
        private IMovableSeries movableSeries;
        private Timer timer;
        private IO_FileMoving fileMoving;

        public event FilesMovedReport filesMovedReport;

        /// <summary>
        /// set timer interval in minutes
        /// </summary>
        public int TimerInterval
        {
            set
            {
                if (value == 0)
                {
                    value = 30;
                }
                timer.Interval = value * 60000;
            }
        }

        public FileMoving(int checkInterval, IMovableSeries getMovableSeries, bool start = true)
        {

            this.movableSeries = getMovableSeries;   
            timer = new Timer();
            if (checkInterval == 0)
            {
                checkInterval = 15;
            }
            timer.Interval = (checkInterval * 60000);
            timer.Elapsed += new ElapsedEventHandler(timeElapsed);
            if (start) timer.Start();
        }

        public void start()
        {
            timer.Start();
        }

        public void pause()
        {
            timer.Stop();
        }

        private void timeElapsed(object sender, EventArgs e)
        {
            Thread t = new Thread(delegate (object unused)
                                      {
                                          fileMoving = new IO_FileMoving(IO_Settings.GetSettings().RootFolder);
                                          filesMovedReport(fileMoving.moveFiles(movableSeries.MoveableSeries()));
                                      }
            );
            t.IsBackground = true;
            t.Start();
        }

        /// <summary>
        /// check files independent of the timer (when Running a timer)
        /// </summary>
        public void checkFiles()
        {
            bool wasEnabled = timer.Enabled;
            if(timer.Enabled) timer.Stop();

            timeElapsed(new object(), null);

            if(wasEnabled) timer.Start();
        }

        /// <summary>
        /// check files once (ex. at startup)
        /// </summary>
        /// <param name="manager"></param>
        public static void checkFilesStatic(IMovableSeries movableSeries, FilesMovedReport filesMovedReport)
        {
            Thread t = new Thread(delegate (object unused)
                                    {
                                        IO_FileMoving fileMoving = new IO_FileMoving(IO_Settings.GetSettings().RootFolder);
                                        filesMovedReport(fileMoving.moveFiles(movableSeries.MoveableSeries()));
                                    }
            );
            t.IsBackground = true;
            t.Start();
        }

    }
}
